package com.buss.purchase.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.PersistenceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buss.core.dic.CheckStatus;
import com.buss.core.exception.ServiceException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.purchase.dao.PurchaseOrderDetailDao;
import com.buss.purchase.dao.PurchaseOrderMasterDao;
import com.buss.purchase.model.PurchaseOrderDetail;
import com.buss.purchase.model.PurchaseOrderMaster;
import com.buss.purchase.service.PurchaseOrderService;

@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {
	
	@Autowired
	private PurchaseOrderMasterDao orderMasterDao;
	
	@Autowired
	private PurchaseOrderDetailDao orderDetailDao;
	
	@Override
	public PurchaseOrderMaster saveOrderMaster(PurchaseOrderMaster master)
			throws PersistenceException {
		PurchaseOrderMaster tmpMaster = loadOrderMaster(master.getBranch().getCompanyId(), master.getOrderNumber());
		if(tmpMaster != null){
			throw new PersistenceException("该采购订单号已经存在，请核实!");
		}
		PurchaseOrderMaster orderMaster = orderMasterDao.saveOrderMaster(master);
		for(PurchaseOrderDetail detail : master.getDetails()){
			detail.setMasterId(orderMaster.getId());
			saveOrderDetail(detail);
		}
		return orderMaster;
	}

	@Override
	public void editOrderMaster(PurchaseOrderMaster master,Long[] removeDetailIds)
			throws PersistenceException {
		if(removeDetailIds != null && removeDetailIds.length > 0){
			removeOrderDetail(removeDetailIds);
		}
		for(PurchaseOrderDetail detail : master.getDetails()){
			PurchaseOrderDetail tmpDetail = orderDetailDao.loadOrderDetail(master.getBranch().getCompanyId(), master.getId(), detail.getGoods().getId());
			if(tmpDetail != null){
				detail.setId(tmpDetail.getId());
				detail.setMasterId(master.getId());
				editOrderDetail(detail);
			}else{
				detail.setMasterId(master.getId());
				saveOrderDetail(detail);
			}
		}
		orderMasterDao.editOrderMaster(master);
	}

	@Override
	public void removeOrderMaster(Long id)  throws ServiceException{
		PurchaseOrderMaster orderMaster = orderMasterDao.loadOrderMaster(id);
		if(orderMaster == null){
			throw new ServiceException("没有找到要删除的订单信息!");
		}
		if(!orderMaster.getCheckStatus().equals(CheckStatus.untreated)){
			throw new ServiceException("只能删除状态为未处理的订单信息!");
		}
		orderDetailDao.removeOrderDetailByMasterId(id);
		orderMasterDao.removeOrderMaster(id);
	}

	@Override
	public PurchaseOrderMaster loadOrderMaster(Long id) {
		return orderMasterDao.loadOrderMaster(id);
	}

	@Override
	public PurchaseOrderMaster loadOrderMaster(String companyId,
			String orderNumber) {
		return orderMasterDao.loadOrderMaster(companyId, orderNumber);
	}

	public void updateCheckStatus(Long id,CheckStatus status,Long checkUserId,
			String checkUserName,Date checkDate){
		orderMasterDao.updateCheckStatus(id, status, checkUserId, checkUserName, checkDate);
	}
	
	@Override
	public ResultList<PurchaseOrderMaster> queryOrderMaster(String companyId,Long supplierId,
			Long branchId, Long salesManId, Date orderStartDate,
			Date orderEndDate, CheckStatus checkStatus, Long checkUserId,
			Date checkStartDate, Date checkEndDate, Long createUserId,
			Date createStartDate, Date createEndDate, Paginalbe page,
			OrderBy orderBy) {
		return orderMasterDao.queryOrderMaster(companyId,supplierId, branchId, salesManId, orderStartDate, orderEndDate, checkStatus, checkUserId, checkStartDate, checkEndDate, createUserId, createStartDate, createEndDate, page, orderBy);
	}

	@Override
	public void saveOrderDetail(PurchaseOrderDetail detail) {
//		PurchaseOrderDetail tmpDetail = orderDetailDao.loadOrderDetail(detail.getGoods().getGoods().getCategory().getCompanyId(), detail.getMasterId(), detail.getGoods().getId());
//		if(tmpDetail != null){
//			detail.setQuantity(detail.getQuantity() + tmpDetail.getQuantity());
//			orderDetailDao.editOrderDetail(detail);
//		}else{
			orderDetailDao.saveOrderDetail(detail);
		//}
	}

	@Override
	public void editOrderDetail(PurchaseOrderDetail detail) {
		orderDetailDao.editOrderDetail(detail);
	}

	@Override
	public void saveOrderDetails(List<PurchaseOrderDetail> orderDetails) {
		orderDetailDao.saveOrderDetails(orderDetails);
	}

	@Override
	public void editOrderDetails(List<PurchaseOrderDetail> orderDetails) {
		orderDetailDao.editOrderDetails(orderDetails);
	}

	@Override
	public void removeOrderDetail(Long[] ids) {
		orderDetailDao.removeOrderDetail(ids);
	}

	@Override
	public ResultList<PurchaseOrderDetail> queryOrderDetail(Long masterId,
			Long categoryId, Long goodsId, Paginalbe page) {
		return orderDetailDao.queryOrderDetail(masterId, categoryId, goodsId, page);
	}

}
